package com.wsc.wisdomchina.ai;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.samton.interfaces.IBenIMSpeakCallBack;
import com.samton.mainSDK.IBenChatSDK;
import com.samton.model.Constants;
import com.samton.utils.ToastUtil;
import com.wsc.wisdomchina.R;
import com.yuntongxun.ecsdk.ECDevice;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 接受电话界面
 * Created by syk on 2017/3/1.
 */

public class PhoneActivity extends AppCompatActivity implements IBenIMSpeakCallBack, View.OnClickListener {
    /**
     * 来电名字
     */
    private TextView mPhoneName;
    /**
     * 时间显示图标
     */
    private ImageView mTimeIcon;
    /**
     * 时间显示
     */
    private TextView mTimeHint;
    /**
     * 静音图标
     */
    private ImageView mMuteIcon;
    /**
     * 扬声器图标
     */
    private ImageView mSpeakerIcon;
    /**
     * 收起图标
     */
    private ImageView mHideIcon;
    /**
     * 拒绝图标
     */
    private ImageView mDenyIcon;
    /**
     * 拒绝层
     */
    private RelativeLayout mDenyLay;
    /**
     * 接受图标
     */
    private ImageView mAcceptIcon;
    /**
     * 接受层
     */
    private RelativeLayout mAcceptLay;
    /**
     * 电话层
     */
    private LinearLayout mCallingLay;
    /**
     * 接电话后的操作层
     */
    private LinearLayout mOperationLay;
    /**
     * 通话时间层
     */
    private RelativeLayout mTimeLay;
    /**
     * 来电唯一标识
     */
    private String mCallID = "";
    /**
     * 来电号码
     */
    private String mCallNumber = "";
    /**
     * 是否第一次创建
     */
    private boolean isCreated = false;
    /**
     * 通话时间
     */
    private int connectTime = 0;
    /**
     * 通话时间定时器
     */
    private Timer mTimer = null;
    /** 刷新UI的Handler*/
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            mTimeHint.setText((String)msg.obj);
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_call);
        isCreated = true;
        // 初始化布局
        initView();
        // 初始化数据
        initData();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (isCreated) {
            initData();
        }
    }

    private void initData() {

        // 电话号码
        mCallID = getIntent().getStringExtra(ECDevice.CALLID);
        // 来电昵称
        mCallNumber = getIntent().getStringExtra(ECDevice.CALLER);
        // 设置来电昵称
        mPhoneName.setText(mCallNumber);
        setCallingLay();
    }

    private void initView() {
        mPhoneName = (TextView) findViewById(R.id.mPhoneName);
        mTimeIcon = (ImageView) findViewById(R.id.mTimeIcon);
        mTimeHint = (TextView) findViewById(R.id.mTimeHint);
        mMuteIcon = (ImageView) findViewById(R.id.mMuteIcon);
        mSpeakerIcon = (ImageView) findViewById(R.id.mSpeakerIcon);
        mHideIcon = (ImageView) findViewById(R.id.mHideIcon);
        mDenyIcon = (ImageView) findViewById(R.id.mDenyIcon);
        mDenyLay = (RelativeLayout) findViewById(R.id.mDenyLay);
        mAcceptIcon = (ImageView) findViewById(R.id.mAcceptIcon);
        mAcceptLay = (RelativeLayout) findViewById(R.id.mAcceptLay);
        mCallingLay = (LinearLayout) findViewById(R.id.mCallingLay);
        mTimeLay = (RelativeLayout) findViewById(R.id.mTimeLay);
        mOperationLay = (LinearLayout) findViewById(R.id.mOperationLay);

        mMuteIcon.setOnClickListener(this);
        mSpeakerIcon.setOnClickListener(this);
        mHideIcon.setOnClickListener(this);
        mDenyIcon.setOnClickListener(this);
        mAcceptIcon.setOnClickListener(this);
    }

    /**
     * 来电话的布局更改
     */
    private void setCallingLay() {
        // 隐藏通话时间层
        mTimeLay.setVisibility(View.GONE);
        // 隐藏操作界面
        mOperationLay.setVisibility(View.GONE);
        // 显示接听按钮
        mAcceptLay.setVisibility(View.VISIBLE);
    }

    /**
     * 接听电话后的布局更改
     */
    private void setAcceptLay() {
        // 清空时间
        connectTime = 0;
        // 开启定时器
        if (mTimer == null) {
            mTimer = new Timer();
            mTimer.scheduleAtFixedRate(new RefreshTask(), 0, 1000);
        }
        // 显示通话时间层
        mTimeLay.setVisibility(View.VISIBLE);
        // 显示操作界面
        mOperationLay.setVisibility(View.VISIBLE);
        // 隐藏接听按钮
        mAcceptLay.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 设置静音
            case R.id.mMuteIcon:

                break;
            // 设置扬声器
            case R.id.mSpeakerIcon:

                break;
            // 收起按钮
            case R.id.mHideIcon:

                break;
            // 拒绝按钮
            case R.id.mDenyIcon:
                break;
            // 接听按钮
            case R.id.mAcceptIcon:
                break;
        }
    }

    @Override
    public void onCallEvent(int type) {
        switch (type){
            // 响铃
            case Constants.ON_CALL_ALERTING:
                break;
            // 呼叫中
            case Constants.ON_CALL_PROCEED:
                break;
            // 接听
            case Constants.ON_CALL_ANSWER:
                setAcceptLay();
                break;
            // 呼叫失败
            case Constants.ON_CALL_FAILED:
                break;
            // 通话结束
            case Constants.ON_CALL_RELEASED:
                ToastUtil.showToast("通话结束");
                if (mTimer != null) {
                    mTimer.cancel();
                    mTimer = null;
                }
                this.finish();
                break;
        }
    }

    class RefreshTask extends TimerTask {
        @Override
        public void run() {
            connectTime += 1;
            int hour = connectTime / 3600;
            int minute = (connectTime % 3600) / 60;
            int second = connectTime % 60;
            StringBuilder stringBuilder = new StringBuilder();
            if (hour <10){
                stringBuilder.append("0" + hour + ":");
            }else {
                stringBuilder.append(hour + "");
            }
            if (minute < 10){
                stringBuilder.append("0" + minute + ":");
            }else {
                stringBuilder.append(minute + "");
            }
            if (second < 10){
                stringBuilder.append("0" + second);
            }else {
                stringBuilder.append(second + "");
            }
            Message msg = mHandler.obtainMessage();
            msg.obj = stringBuilder.toString();
            mHandler.sendMessage(msg);
        }
    }
}
